package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.dto.AlertDataHandleDto;
import com.zzyl.nursing.dto.AlertDataPageReqDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

import static javax.management.Query.eq;

/**
 * 报警数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-25
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    @Override
    public List<AlertData> selectAlertDataPage(AlertDataPageReqDto alertDataPageReqDto) {
        Page<AlertData> page = new Page<>(alertDataPageReqDto.getPageNum(), alertDataPageReqDto.getPageSize());

        LambdaQueryWrapper<AlertData> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(alertDataPageReqDto.getDeviceName()), AlertData::getDeviceName, alertDataPageReqDto.getDeviceName())
                .eq(ObjectUtil.isNotEmpty(alertDataPageReqDto.getStatus()), AlertData::getStatus, alertDataPageReqDto.getStatus())
                .eq(ObjectUtil.isNotEmpty(alertDataPageReqDto.getId()), AlertData::getId, alertDataPageReqDto.getId())
                //只有接收人才能看到对应的报警数据
                .eq(AlertData::getUserId, SecurityUtils.getUserId())
                .between(ObjectUtil.isNotNull(alertDataPageReqDto.getStartTime()) && ObjectUtil.isNotNull(alertDataPageReqDto.getEndTime()), AlertData::getCreateTime, alertDataPageReqDto.getStartTime(), alertDataPageReqDto.getEndTime());
        page = page(page, queryWrapper);

        return page.getRecords();

    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public void handleAlertDataById(AlertDataHandleDto alertDataHandleDto) {

        //根据报警数据id获取到该数据的iotId和functionId和createTime，最后对符合这三个值的数据进行更新
        AlertData alertData = getById(alertDataHandleDto.getId());

        LambdaUpdateWrapper<AlertData> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.eq(AlertData::getIotId, alertData.getIotId())
                .eq(AlertData::getFunctionId, alertData.getFunctionId())
                .eq(AlertData::getCreateTime, alertData.getCreateTime())
                .set(AlertData::getProcessingResult, alertDataHandleDto.getProcessingResult())
                .set(AlertData::getStatus, 1)
                .set(AlertData::getProcessingTime, alertDataHandleDto.getProcessingTime())
                //处理人为当前用户
                .set(AlertData::getProcessorId, SecurityUtils.getUserId())
                .set(AlertData::getProcessorName, SecurityUtils.getUsername());
        update(updateWrapper);

    }
}
